package com.example.rule.util;


import com.example.rule.db.entity.PymtRule;
import com.example.rule.db.repository.PymtRuleRepository;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author Jason - Chusifeng
 * @version 1.0.0
 * @className CacheListener.java
 * @packageName com.example.rule.util
 * @createTime 2022年10月10日 10:22:58
 * @description 本地缓存被移除后的监听类
 */
@Slf4j

public class CacheListener implements RemovalListener {

    private PymtRuleRepository ruleRepository;

    @Override
    public void onRemoval(RemovalNotification removalNotification) {
        if (ruleRepository == null) {
            ruleRepository = SpringApplicationUtils.getApplicationContext().getBean(PymtRuleRepository.class);
        }
        log.info("local cache remove,key{},cause {}", removalNotification.getKey(), removalNotification.getCause());
        List<PymtRule> ruleList = ruleRepository.findAll();
        CacheUtil.cacheRule(ruleList);
    }

}
